package com.leyou.user.service;

import com.leyou.common.auth.pojo.UserHolder;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.user.entity.DeliveryAddress;
import com.leyou.user.mapper.DeliveryAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import java.util.List;

@Service
@Transactional
public class DeliveryAddressService {
    @Autowired
    private DeliveryAddressMapper deliveryAddressMapper;

    /**
     * 查询用户所有收货地址
     * @return
     */
    public List<DeliveryAddress> queryUserAllDeliveryAddress() {
        DeliveryAddress record = new DeliveryAddress();
        record.setUid(UserHolder.getUserId());//正式使用，还需导入拦截器配置
//        record.setUid(32L);//测试用
        List<DeliveryAddress> deliveryAddressList = deliveryAddressMapper.select(record);
        if (CollectionUtils.isEmpty(deliveryAddressList)){
            throw new LyException(404,"用户收货地址不存在");
        }
        return deliveryAddressList;
    }

    /**
     * 新增收货地址
     * @param deliveryAddress
     */
    public void addUserDeliveryAddress(DeliveryAddress deliveryAddress) {
        deliveryAddress.setUid(UserHolder.getUserId());//正式使用，还需导入拦截器配置
//        deliveryAddress.setUid(32L);
        try {
            deliveryAddressMapper.insertSelective(deliveryAddress);
        } catch (Exception e) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }
    /**
     * 根据id查询收货地址
     */
    public DeliveryAddress queryDeliveryAddressById(Long id) {
        DeliveryAddress deliveryAddress = deliveryAddressMapper.selectByPrimaryKey(id);
        if (deliveryAddress==null){
            throw new LyException(404,"用户收货地址不存在");
        }
        return deliveryAddress;
    }
    /**
     * 删除收货地址
     */
    public void deleteDeliveryAddressById(Long id) {

        int count = deliveryAddressMapper.deleteByPrimaryKey(id);
        if (count==0){
            throw  new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }
    /**
     * 更新收货地址
     */
    public void updateDeliveryAddress(DeliveryAddress deliveryAddress) {
        try {
            deliveryAddressMapper.updateByPrimaryKeySelective(deliveryAddress);
        } catch (Exception e) {
            throw  new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    /**
     * 查询是否可设置默认地址
     */
    public Boolean queryHasIsDefault() {
        DeliveryAddress record = new DeliveryAddress();
        record.setIsDefault(true);
        DeliveryAddress deliveryAddress = deliveryAddressMapper.selectOne(record);
        if (deliveryAddress==null){
            return true;
        }
        return false;
    }

    public void updateDefaultDeliveryAddress(DeliveryAddress deliveryAddress) {
        DeliveryAddress record = new DeliveryAddress();
        record.setIsDefault(true);
        DeliveryAddress defaultDeliveryAddress = deliveryAddressMapper.selectOne(record);
        defaultDeliveryAddress.setIsDefault(false);
        deliveryAddressMapper.updateByPrimaryKeySelective(defaultDeliveryAddress);
        deliveryAddress.setIsDefault(true);
        deliveryAddressMapper.updateByPrimaryKeySelective(deliveryAddress);

    }
}
